from selenium import webdriver
from selenium.webdriver.common.by import By
import time

# 页面切换

driver = webdriver.Edge()

url = "https://www.baidu.com/"

driver.get(url)
driver.maximize_window()
driver.find_element(By.ID,"kw").send_keys("美女")
time.sleep(2)
driver.find_element(By.ID,"su").click()
# 数组存储窗口
print(driver.window_handles)
# 当前窗口
print(driver.current_window_handle)

time.sleep(2)

driver.implicitly_wait(5)
driver.find_element(By.CSS_SELECTOR,"[srcid='4']").click()

print(driver.window_handles)
print(driver.current_window_handle)

driver.implicitly_wait(5)
# 页面切换
driver.switch_to.window(driver.window_handles[1])
print(driver.window_handles)
print(driver.current_window_handle)
driver.find_element(By.XPATH,"//*[@id='srcPic']/img").click()

# 截图
driver.save_screenshot("img.png")

# 添加cookie 添加一个字典数据
driver.add_cookie({"name":"lyr","value":"111"})

time.sleep(5)
driver.quit()